package cn.enilu.flash.service.park;


import cn.enilu.flash.bean.entity.park.ParkingQuoteRef;
import cn.enilu.flash.bean.view.ParkQuoteRefView;
import cn.enilu.flash.bean.view.ParkQuoteView;
import cn.enilu.flash.dao.park.ParkingQuoteRefRepository;

import cn.enilu.flash.service.BaseService;
import cn.enilu.flash.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ParkingQuoteRefService extends BaseService<ParkingQuoteRef,Long,ParkingQuoteRefRepository>  {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ParkingQuoteRefRepository parkingQuoteRefRepository;

    @Transactional(rollbackFor = Exception.class)
    public void saveAll(ParkQuoteRefView parkQuoteRefView){
        parkingQuoteRefRepository.deleteByParkingId(parkQuoteRefView.getParkingId());
        String idStr = parkQuoteRefView.getIds();
        if(StringUtils.isNotNullOrEmpty(idStr)){
            ParkingQuoteRef ref = null;
            String[] ids = idStr.split("_");
            for (String s : ids) {
                ref = new ParkingQuoteRef();
                ref.setParkingId(parkQuoteRefView.getParkingId());
                ref.setQuoteId(Long.parseLong(s));
                ref.setCreateBy(1L);
                ref.setCreateTime(new Date());
                ref.setModifyBy(1L);
                ref.setModifyTime(new Date());
                parkingQuoteRefRepository.save(ref);
            }
        }
    }

    public List<ParkQuoteView> getAllQuotes(Long communityId, Long parkingId){
        List<Map<String, Object>> list = parkingQuoteRefRepository.getAllQuotes(communityId, parkingId);
        List<ParkQuoteView> result = null;
        ParkQuoteView view = null;
        if(list != null && !list.isEmpty()){
            result = new ArrayList<>();
            for (Map<String, Object> map : list) {
                view = new ParkQuoteView();
                view.setId(((BigInteger)map.get("id")).longValue());
                view.setQuoteType((Integer)map.get("quoteType"));
                view.setQuoteRange((Integer)map.get("quoteRange"));
                view.setQuoteUnit((Integer)map.get("quoteUnit"));
                view.setQuotePrice((Double)map.get("quotePrice"));
                view.setChecked1(((BigInteger)map.get("checked1")).intValue());
                view.setChecked2(((BigInteger)map.get("checked2")).intValue());
                view.setStartDate((Date)map.get("startDate"));
                view.setEndDate((Date)map.get("endDate"));
                result.add(view);
            }
        }
        return result;
    }

}

